package net.orange.console;

import java.io.PrintStream;

import net.orange.console.genius.DepartmentGenius;
import net.orange.console.genius.FileGenius;
import net.orange.console.genius.GroupGenius;
import net.orange.console.genius.UserGenius;

import org.apache.bsf.BSFManager;

/**
 * The system console which can explore the internal java program. it can run
 * the dynamic scripting program in it. and it had registered some java class
 * which can them directly:<br>
 * 
 * 1: $console - the system object.<br>
 * 2: $department - the delegate of IDepartmentProcess object.<br>
 * 3: $group - the delegate of IGroupProcess object.<br>
 * 4: $user - the delegate of IUserProcess object.<br>
 * 5: $file - the file operation delegate object.
 */
public class SystemConsole {

	/**
	 * Bean Scripting Framework(BSF) instance which can provide the language
	 * support.
	 */
	private BSFManager manager;
	/**
	 * The script language type
	 */
	private static String SCRIPT_LANUAGE = "javascript";
	/**
	 * The register name of System instance delegate.
	 */
	private static String REG_CURR_CONSOLE = "$console";
	/**
	 * The register name of IDepartmentProcess instance delegate.
	 */
	private static String REG_GENIUS_DEPARTMENT = "$department";
	/**
	 * The register name of IGroupProcess instance delegate.
	 */
	private static String REG_GENIUS_GROUP = "$group";
	/**
	 * The register name of IUserProcess instance delegate.
	 */
	private static String REG_GENIUS_USER = "$user";
	/**
	 * The register name of file operation delegate.
	 */
	private static String REG_FILE_INSTANCE = "$file";

	/**
	 * Default constructor which initialize the system console.
	 * 
	 * @throws Exception
	 */
	public SystemConsole() throws Exception {
		manager = new BSFManager();
		initialize();
	}

	/**
	 * Initialize all the genius.
	 * 
	 * @throws Exception
	 */
	private void initialize() throws Exception {
		manager.declareBean(REG_CURR_CONSOLE, System.out, PrintStream.class);

		manager.declareBean(REG_GENIUS_DEPARTMENT, DepartmentGenius
				.getInstance(), DepartmentGenius.class);
		manager.declareBean(REG_GENIUS_GROUP, GroupGenius.getInstance(),
				GroupGenius.class);
		manager.declareBean(REG_GENIUS_USER, UserGenius.getInstance(),
				UserGenius.class);

		manager.declareBean(REG_FILE_INSTANCE, FileGenius.getInstance(),
				FileGenius.class);
	}

	/**
	 * Execute the scripting program.
	 * 
	 * @param arg
	 *            The scripting program.
	 * @throws Exception
	 */
	public void execute(String arg) throws Exception {
		if (arg != null && arg.length() > 0)
			manager.exec(SCRIPT_LANUAGE, "tt", 0, 0, formatScript(arg));
	}

	/**
	 * Execute the scripting program and return the value
	 * 
	 * @param arg
	 *            The scripting program.
	 * @return The return value of the program.
	 * @throws Exception
	 */
	public Object eval(String arg) throws Exception {
		return (arg != null && arg.length() > 0) ? manager.eval(SCRIPT_LANUAGE,
				"tt", 0, 0, formatScript(arg)) : null;
	}

	/**
	 * format the scripting program (replace the html tag.)
	 * 
	 * @param arg
	 *            The scripting program.
	 * @return The scripting program after format.
	 */
	private String formatScript(String arg) {
		if (arg != null) {
			arg = arg.replaceAll("&gt;", ">");
			arg = arg.replaceAll("&lt;", "<");
		}
		return arg;
	}
}
